
package com.bootx.service.impl;

import com.bootx.common.Filter;
import com.bootx.common.Order;
import com.bootx.common.Page;
import com.bootx.common.Pageable;
import com.bootx.dao.ArticleCategoryDao;
import com.bootx.dao.ArticleDao;
import com.bootx.dao.ArticleTagDao;
import com.bootx.entity.Article;
import com.bootx.entity.ArticleCategory;
import com.bootx.entity.ArticleTag;
import com.bootx.service.ArticleCategoryService;
import com.bootx.service.ArticleService;
import com.bootx.service.ArticleTagService;
import jakarta.annotation.Resource;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * Service - 文章
 * 
 * @author 好源++ Team
 * @version 6.1
 */
@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article, Long> implements ArticleService {

	@PersistenceContext
	private EntityManager entityManager;

	@Resource
	private ArticleDao articleDao;
	@Resource
	private ArticleCategoryDao articleCategoryDao;
	@Resource
	private ArticleTagDao articleTagDao;

    @Resource
    private ArticleCategoryService articleCategoryService;

    @Resource
    private ArticleTagService articleTagService;

	@Override
	@Transactional(readOnly = true)
	public List<Article> findList(ArticleCategory articleCategory, ArticleTag articleTag, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders) {
		return articleDao.findList(articleCategory, articleTag, isPublication, count, filters, orders);
	}

	@Override
	@Transactional(readOnly = true)
	public List<Article> findList(Long articleCategoryId, Long articleTagId, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders, boolean useCache) {
		ArticleCategory articleCategory = articleCategoryDao.find(articleCategoryId);
		if (articleCategoryId != null && articleCategory == null) {
			return Collections.emptyList();
		}
		ArticleTag articleTag = articleTagDao.find(articleTagId);
		if (articleTagId != null && articleTag == null) {
			return Collections.emptyList();
		}
		return articleDao.findList(articleCategory, articleTag, isPublication, count, filters, orders);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Article> findPage(ArticleCategory articleCategory, ArticleTag articleTag, Boolean isPublication, Pageable pageable) {
		return articleDao.findPage(articleCategory, articleTag, isPublication, pageable);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<Article> search(String keyword, Pageable pageable) {
		if (StringUtils.isEmpty(keyword)) {
			return Page.emptyPage(pageable);
		}

		if (pageable == null) {
			pageable = new Pageable();
		}
		return new Page<>(Collections.emptyList(), 0L, pageable);
	}

	@Override
	public long viewHits(Long id) {
		Assert.notNull(id, "[Assertion failed] - id is required; it must not be null");

		return 0L;
	}

    @Override
    public void create(Article article) {
        /*ArticleCategory articleCategory = articleCategoryService.find(1L);
        article.setArticleCategory(articleCategory);
        List<ArticleTag> articleTags = articleTagService.createAll(article.getArticleTags());
        article.setArticleTags(new HashSet<>(articleTags));*/
        // 保存稿件
        jdbcTemplate.update("insert into article(createdDate, lastModifiedDate, version, author, content, cover, memo, otherId, publishDate, seoDescription, seoKeywords, seoTitle, source, title, url, articleCategory_id) VALUES (NOW(),NOW(),0,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                article.getAuthor(),article.getContent(),article.getCover(),article.getMemo(),article.getOtherId(),article.getPublishDate(),article.getSeoDescription(),article.getSeoKeywords(),article.getSeoTitle(),article.getSource(),article.getTitle(),article.getUrl(),1);
        // 根据otherId找到这个稿件的id

    }

}